/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClienteBrowser.java
 *
 * Created on 17/05/2011, 21:07:37
 */
package br.com.softnight.ui;

import br.com.softnight.dao.ClienteDAO;
import br.com.softnight.db.DBUtil;
import br.com.softnight.entidades.Cliente;
import br.com.softnight.ui.tablemodel.TableModelBrowserCliente;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TableModelEvent;
import javax.swing.table.TableColumn;

/**
 *
 * @author Franklin
 */
public class ClienteBrowser extends javax.swing.JDialog {

    private TableModelBrowserCliente tableModelBrowserCliente = new TableModelBrowserCliente();
    private TableColumn tableColumnId = new TableColumn(0, 10);
    private TableColumn tableColumnCpf = new TableColumn(1, 40);
    private TableColumn tableColumnTelefone = new TableColumn(2, 40);
    private TableColumn tableColumnNome = new TableColumn(3, 200);

    /** Creates new form ClienteBrowser */
    public ClienteBrowser(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();

        this.jTable1.setModel(this.tableModelBrowserCliente);

        this.tableColumnId.setHeaderValue("id");
        this.tableColumnCpf.setHeaderValue("CPF");
        this.tableColumnTelefone.setHeaderValue("Telefone");
        this.tableColumnNome.setHeaderValue("Nome");
        
        this.jTable1.addColumn(this.tableColumnId);
        this.jTable1.addColumn(this.tableColumnCpf);
        this.jTable1.addColumn(this.tableColumnTelefone);
        this.jTable1.addColumn(this.tableColumnNome);
    }

    public void abre() throws SQLException {
        this.tableModelBrowserCliente.atualiza();
        
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        btnInsere = new javax.swing.JButton();
        btnEdita = new javax.swing.JButton();
        btnExclui = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new br.com.softnight.ui.tablemodel.TableModelBrowserCliente());
        jScrollPane1.setViewportView(jTable1);

        btnInsere.setText("Insere");
        btnInsere.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInsereActionPerformed(evt);
            }
        });

        btnEdita.setText("Edita");
        btnEdita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditaActionPerformed(evt);
            }
        });

        btnExclui.setText("Exclui");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnInsere)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEdita)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnExclui)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 283, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInsere)
                    .addComponent(btnEdita)
                    .addComponent(btnExclui))
                .addGap(11, 11, 11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnInsereActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInsereActionPerformed
        ClienteEdicao clienteEdicao = new ClienteEdicao(null, true);
        try {
            Cliente cliente = new Cliente();
            cliente.setId(DBUtil.nextID());
            clienteEdicao.edita(cliente);

            ClienteDAO clienteDAO = new ClienteDAO();
            clienteDAO.insere(cliente);

            this.tableModelBrowserCliente.atualiza();
            this.jTable1.tableChanged(new TableModelEvent(this.jTable1.getModel()));
        } catch (SQLException ex) {
            Logger.getLogger(ClienteBrowser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnInsereActionPerformed

    private void btnEditaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditaActionPerformed
        Long id = (Long) this.tableModelBrowserCliente.getValueAt(this.jTable1.getSelectedRow(), 0);

        ClienteEdicao clienteEdicaoUI = new ClienteEdicao(null, true);
        try {
            ClienteDAO clienteDAO = new ClienteDAO();
            Cliente cliente = clienteDAO.consultaPorId(id);
            clienteEdicaoUI.edita(cliente);
            
            clienteDAO.atualiza(cliente);
                    
            this.tableModelBrowserCliente.atualiza();
            this.jTable1.tableChanged(new TableModelEvent(this.jTable1.getModel()));
        } catch (SQLException ex) {
            Logger.getLogger(ClienteBrowser.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnEditaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEdita;
    private javax.swing.JButton btnExclui;
    private javax.swing.JButton btnInsere;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
